package com.conversationboard.model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.conversationboard.database.Database;
import com.conversationboard.database.Queries;
import com.conversationboard.logger.LogMessageType;

/**
 * Class to get the errors from the database and display the most recent stack
 * traces for debugging purposes. Only available to site admins.
 * 
 * @author Keith Watson
 * 
 */

public class Log {

	private Log() {
	}


	public static List<LogMessage> getLog(LogMessageType logMessageType) throws SQLException {

		Connection connection = Database.getConnection();

		List<LogMessage> log = new ArrayList<LogMessage>(50);

		try {
			PreparedStatement statement = connection.prepareStatement(Queries.GET_LOG_SUBSET);
			statement.setString(1, logMessageType.toString());
			statement.execute();

			ResultSet resultSet = statement.getResultSet();

			while (resultSet.next()) {

				LogMessage error = new LogMessage();
				error.setTimestamp(resultSet.getTimestamp("timestamp"));
				error.setMessage(resultSet.getString("message"));
				error.setLoginId(resultSet.getString("loginid"));
				error.setType(LogMessageType.valueOf(resultSet.getString("type")));

				log.add(error);
			}

			statement.close();

			return log;

		} finally {
			Database.close(connection);
		}

	}


	public static List<LogMessage> getLog() throws SQLException {

		Connection connection = Database.getConnection();

		List<LogMessage> log = new ArrayList<LogMessage>(50);

		try {
			PreparedStatement statement = connection.prepareStatement(Queries.GET_LOG);
			statement.execute();

			ResultSet resultSet = statement.getResultSet();

			while (resultSet.next()) {

				LogMessage error = new LogMessage();
				error.setTimestamp(resultSet.getTimestamp("timestamp"));
				error.setMessage(resultSet.getString("message"));
				error.setLoginId(resultSet.getString("loginid"));
				error.setType(LogMessageType.valueOf(resultSet.getString("type")));

				log.add(error);
			}

			statement.close();

			return log;

		} finally {
			Database.close(connection);
		}
	}


	/**
	 * Delete everything from the log
	 * 
	 * @throws SQLException
	 */

	public static void clearOldUserLog() throws SQLException {

		synchronized (Log.class) {

			Connection connection = null;

			try {

				connection = Database.getConnection();

				PreparedStatement statement = connection.prepareStatement(Queries.CLEAR_OLD_USER_LOGS);
				statement.executeUpdate();

				statement.close();

			} finally {
				Database.close(connection);
			}
		}

	}


	public static void clear(LogMessageType logMessageType) throws SQLException {

		synchronized (Log.class) {

			Connection connection = null;

			try {

				connection = Database.getConnection();

				PreparedStatement statement = connection.prepareStatement(Queries.CLEAR_LOG);
				statement.setString(1, logMessageType.toString());

				statement.executeUpdate();

				statement.close();

			} finally {
				Database.close(connection);
			}
		}

	}

}
